package com.hjt.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.hjt.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.hjt.gulimall.product.dao.AttrDao;
import com.hjt.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hjt.gulimall.product.entity.AttrEntity;
import com.hjt.gulimall.product.entity.ProductAttrValueEntity;
import com.hjt.gulimall.product.service.AttrService;
import com.hjt.gulimall.product.service.ProductAttrValueService;
import com.hjt.gulimall.product.vo.AttrGroupRelationVo;
import com.hjt.gulimall.product.vo.AttrGroupWithAttrVo;
import com.hjt.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.Query;

import com.hjt.gulimall.product.dao.AttrGroupDao;
import com.hjt.gulimall.product.entity.AttrGroupEntity;
import com.hjt.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.swing.plaf.basic.BasicEditorPaneUI;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(String catelogId, Map<String, Object> params) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        // 根据分组id查询和模糊查询名称
        if (StringUtils.hasLength(key)) {
            wrapper.and((obj) -> {
                // key不为空，则查attr_group_id 或者 模糊查 attr_group_name
                obj.eq("attr_group_id",key).or().like("attr_group_name", key);
            });
        }
        // 根据catelog_id查询
        if (!catelogId.equals("0")) {
            wrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new  Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 删除关联关系
     * @param params
     */
    @Override
    public void deleteRelation(List<AttrGroupRelationVo>params) {

        // 遍历删除关系
        params.forEach(param -> {
            attrAttrgroupRelationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_id", param.getAttrId())
                    .eq("attr_group_id", param.getAttrGroupId()));
        });
    }

    /**
     * 根据分类id，查出该分类下的所有分组以及每个分组下的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 查出所有分组
        List<AttrGroupEntity> groupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 查询每个分组的所有属性
        List<AttrGroupWithAttrVo> vos = groupEntities.stream().map(group -> {
            AttrGroupWithAttrVo attrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(group, attrVo);
            // 查询所有属性id
            // 查询设置分组关联的所有属性
            attrVo.setAttrs(attrService.getRelationAttr(group.getAttrGroupId()));
            return attrVo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public List<SkuItemVo.SpuItemBaseGroupVo> getAttrGroupWithAttrsBySpuIdAndCatalogId(Long spuId, Long catalogId) {
        // 1.查出分类下的所有属性分组
//        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));
//        // 2.查出分组-属性关系表(分组id-属性id)
//        List<Long> attrGroupIdList = attrGroupEntityList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
//        List<AttrAttrgroupRelationEntity> attrGroupRelationEntityList =  attrAttrgroupRelationDao.listByGroupIds(attrGroupIdList);
//
//        // 3.封装每个分组下的所有属性
//        attrGroupEntityList.stream().map(attrGroupEntity -> {
//            SkuItemVo.SpuItemBaseGroupVo spuItemBaseGroupVo = new SkuItemVo.SpuItemBaseGroupVo();
//            spuItemBaseGroupVo.setGroupName(attrGroupEntity.getAttrGroupName());
//            attrGroupRelationEntityList.stream().
//        })
        return baseMapper.selectAttrGroupWithAttrs(spuId,catalogId);
    }

}